package com.pubpage.flow.flow_note_add;

import com.pub.database.*;
import com.pub.tools.PublicFunction;

import java.util.Vector;

public class Business {
   QueryBean gqbnDAO=null;

   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }

   String exeInsSQL(View view, String pstrUserNO) throws Exception {
       String lstrSQL = new String();
       Vector<Object> lvtrParam = new Vector<Object>();
       try {
           if(view.getFlow_code()==null){
               lvtrParam.add(view.getFlow_num().substring(0, 3));
           }else{lvtrParam.add(view.getFlow_code());}
           lvtrParam.add(view.getFlow_num());
           lvtrParam.add(view.getFlow_no());
           lvtrParam.add(view.getRout_no());
           lvtrParam.add(PublicFunction.getTimestempDate(PublicFunction.getSYSDATE("yyyy/MM/dd HH:mm:ss")));
           lvtrParam.add(view.getR_empno());
           lvtrParam.add(view.getSign_note());
           lvtrParam.add(view.getNote_kind());

           lstrSQL = "insert into wkf_log (flow_code,flow_num,flow_no,rout_no,sign_date,employee_no,sign_note,r_flow_no,r_employee_no,log_st,reject_no) values(?,?,?,?,?,?,?,'00','ALL',?,'00')";

           if(gqbnDAO.executeSQL(lstrSQL,lvtrParam)){
               return "success";
           }
           else{
               throw new Exception("wkf_log");
           }
       } catch (Exception e) {
           e.printStackTrace();
           throw new Exception("存檔錯誤==>" + e.toString());
       } finally {
           lvtrParam.clear();
       }
    }

   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view, String pstrUserNO) throws Exception{

       return "";
    }

    /**
     * setButtonShow
     *
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserID String
     */
    void setButtonShow(View view,String pstrPrgAction, String pstrPrgStatus,
                       String pstrUserID) throws Exception{
    }

    void clearData(View view) throws Exception{
    }

    /**
     * setReadonlyFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     */
    void setReadonlyFields(View view, String pstrPrgAction,
                           String pstrPrgStatus, String pstrUserNO) {
    }

    /**
     * setShowFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     */
    void setShowFields(View view, String pstrPrgAction, String pstrPrgStatus,
                       String pstrUserNO) {
    }
    /**
     * initAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    String initAction(View view, String pstrUserNO) {
        if(view.getNote_kind()!=null && view.getNote_kind().equals("L")){//approve
            view.setPrgTitle("填寫審核意見：");

            view.setTd_header_css("td_head_1");
            view.setTd_content_css("td_content_1");

            if(view.getRights()!=null && view.getRights().equals("0")){//無權限寫審核意見
                view.setOnloadScript("alert('此站無權限填寫審核意見');window.close();");
                return "";
            }
        }
        else if(view.getNote_kind()!=null && view.getNote_kind().equals("N")){//reject
            view.setTd_header_css("td_head");
            view.setTd_content_css("td_content");

            view.setPrgTitle("填寫駁回原因：");
            view.setPrgNote("請填寫駁回原因，填寫完畢按「確定」鍵立即駁回。");
        }

        view.setOnloadScript("set_window_on_middle(window.window);");

        return "";
    }

    /**
     * saveAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    String saveAction(View view, String pstrUserNO) throws Exception{
        if(exeInsSQL(view,pstrUserNO).equals("success")){
            view.setOnloadScript("alert('存檔完成!!!');window.returnValue='success';window.close();");
        }
        return "";
    }
}
